package com.jike.jkgame.data;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

public class Props {
	public int id;
	public String name;
	public String descript;
	public byte type;
	public int price;
	
	public static final int FILE_TYPE = 3;
	private static int version = 1;
	
	public static int getVersion() {
		return version;
	}
	
	private static Props getProps(DataInputStream dis) {
		Props prop = new Props();
		
		byte[] buff = new byte[4096];
		
		try {
			prop.id = dis.readInt();
			int len = dis.readInt();
			dis.read(buff, 0, len);
			prop.name = new String(buff, 0, len, "gb2312");
			
			len = dis.readInt();
			dis.read(buff, 0, len);
			prop.descript = new String(buff, 0, len, "gb2312");
			
			prop.type = dis.readByte();
			prop.price = dis.readInt();
			
		} catch (IOException e) {
			return null;
		}
		
		return prop;
	}
	
	public static List<Props> parsPropses(InputStream in) {
		List<Props> propses = new LinkedList<Props>();
		
		DataInputStream dis = new DataInputStream(in);
		try {
			int type = dis.readInt();
			
			if (type != FILE_TYPE) {
				return null;
			}
			version = dis.readInt();
			Props props = null;
			do {
				props = getProps(dis);
				propses.add(props);
			}while(props != null);
			
		} catch (IOException e) {
			return null;
		}
		
		return propses;
	}
	
}
